<?php

namespace App\Service\Adolescent;

use App\Model\AdolescentPassword;
use App\Service\BaseService;

class SettingAdolescentService extends BaseService
{
    /**
     * 设置青少年模式
     * @return int[]
     */
    public function handle(): array
    {
        $userId = $this->getUserId();
        if (!$userId) {
            return $this->setError(1);
        }

        if (($password = $this->request->input('password', '')) == '') {
            return $this->setError(0, '请输入密码');
        }

        $adolescentPasswordModel = new AdolescentPassword();
        if ($adolescentPasswordModel->checkIsPassword($userId) == 1) {
            return $this->setError(0, '已开启开启青少年模式');
        }

        $adolescentPasswordModel->user_id = $userId;
        $adolescentPasswordModel->password = $password;
        $adolescentPasswordModel->created_at = date('Y-m-d H:i:s');

        if (!$adolescentPasswordModel->save()) {
            return $this->setError(0, '开启青少年模式失败');
        }
        return ['result' => 1];
    }
}